package cn.wolfcode.luowowo.website.util;

import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 统一异常处理
 * @ControllerAdvice: 给controller做增强
 */
@ControllerAdvice
public class CommonExceptionHandler{
    //专门处理自定义的业务异常
    @ExceptionHandler(LogicException.class)
    public void logincExceptionHandler(Exception e, HttpServletResponse resp) throws IOException {
        e.printStackTrace();
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSONString(new AjaxResult(false,e.getMessage())));
    }
    //处理其他代码异常
    @ExceptionHandler(Exception.class)
    public void exceptionHanler(Exception e, HttpServletResponse resp) throws IOException {
        e.printStackTrace();
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSONString(new AjaxResult(false,"系统出现未知异常，请联系客服")));
    }
}
